package base

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	clientv3 "go.etcd.io/etcd/client/v3"
)

type CmdbConfig struct {
	Http string `json:"http"`
}

func NewClient() {
	client, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"127.0.0.1:2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		panic(err)
	}

	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
	resp, err := client.MemberList(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println(resp)

	cmdbConf := &CmdbConfig{Http: "8080"}
	ConfJson, _ := json.Marshal(cmdbConf)

	objectKey := "/registry/configs/default/cmdb"
	client.Put(ctx, objectKey, string(ConfJson))

	GetResp, err := client.Get(ctx, objectKey)
	if err != nil {
		panic(err)
	}
	for i := range GetResp.Kvs {
		item := GetResp.Kvs[i]
		fmt.Print(string(item.Key), string(item.Value))
	}

	client.Delete(ctx, objectKey)

}
